package com.tenglong.teacher.ui.dialog;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import com.cuotibao.zhuoxiao.basics.utils.ToastUtils;
import com.tenglong.teacher.R;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class WheelDialogFragment extends BaseFromBottomToShowDialogFragment implements View.OnClickListener {

    public static final String KEY_WHEEL_DATA = "key_wheel_data";
    private ArrayWheelAdapter mAdapter;

    public static WheelDialogFragment getInstance(ArrayList<String> mOptionsItems){
        WheelDialogFragment f = new WheelDialogFragment();
        Bundle param = new Bundle();
        param.putStringArrayList(KEY_WHEEL_DATA,mOptionsItems);
        f.setArguments(param);
        return f;
    }


    private WheelView wheelView;
    private List<String> infos;
    private OnItemSelectedListener listener;

    public WheelDialogFragment() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (getArguments().containsKey(KEY_WHEEL_DATA)){
            infos = getArguments().getStringArrayList(KEY_WHEEL_DATA);
            if (infos == null){
                dismiss();
            } else {
                if (infos.size() == 0){
                    ToastUtils.showCenterToast("没有可选项");
                    dismiss();
                }
            }
        } else {
            dismiss();
        }
    }

    @Override
    protected View createView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_wheel_dialog, container, false);
    }

    @Override
    protected void viewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        wheelView = view.findViewById(R.id.wheelView);
        wheelView.setCyclic(false);
        mAdapter = new ArrayWheelAdapter(infos);
        wheelView.setAdapter(mAdapter);
        view.findViewById(R.id.cancle).setOnClickListener(this);
        view.findViewById(R.id.comfirm).setOnClickListener(this);
    }


    public void setOnItemSelectedListener(OnItemSelectedListener listener){
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.comfirm:
                if (listener!=null){
                    listener.onItemSelected(wheelView.getCurrentItem());
                }
            case R.id.cancle:
                dismiss();
                break;
        }
    }
}
